package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.businessproduct.pojo.dto.BusinessProductBatchListDTO;
import com.xbongbong.pro.businessproduct.pojo.dto.BusinessProductListDTO;
import com.xbongbong.pro.businessproduct.pojo.dto.BusinessProductListRelyDTO;
import com.xbongbong.pro.businessproduct.pojo.dto.BussinessProductBatchAutoMatchDTO;
import com.xbongbong.pro.businessproduct.pojo.vo.BusinessProductBatchListVO;
import com.xbongbong.pro.businessproduct.pojo.vo.BusinessProductListRelyVO;
import com.xbongbong.pro.businessproduct.pojo.vo.BusinessProductListVO;
import com.xbongbong.pro.businessproduct.pojo.vo.RefundAmountVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.BusinessProductService;
import com.xbongbong.saas.service.RefundService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 业务关联产品信息的controller
 *
 * @author lcx
 * @version v1.0
 * @date 2019/2/25 10:48
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/business/product")
public class BusinessProductController {

    @Resource
    private BusinessProductService businessProductService;
    @Resource
    private RefundService refundService;

    /**
     * 选择业务关联产品列表
     *
     * @param businessProductListDTO
     * @author chaoxiong.lei
     * @Date 19:29 2019/3/7
     * @since v1.0
     * @return java.lang.String
     **/
    @RequestMapping(value = "/productList/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String businessProductListGet(@RequestBody @Valid BusinessProductListDTO businessProductListDTO, BindingResult br) {
        XbbResponse<BusinessProductListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(businessProductListDTO.getSaasMark(), businessProductListDTO.getBusinessType(), businessProductListDTO.getMenuId(), businessProductListDTO.getDistributorMark());
                boolean isDetail = false;
                businessProductListDTO.setDetailGet(isDetail);
                BusinessProductListVO businessProductListVO = businessProductService.getBusinessProductList(businessProductListDTO);
                response = new XbbResponse<>(businessProductListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * 选择业务关联产品列表
     *
     * @param businessProductListRelyDTO
     * @author chaoxiong.lei
     * @Date 19:29 2019/3/7
     * @since v1.0
     * @return java.lang.String
     **/
    @RequestMapping(value = "/productList/getRely", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String businessProductListGetRely(@RequestBody @Valid BusinessProductListRelyDTO businessProductListRelyDTO, BindingResult br) {
        XbbResponse<BusinessProductListRelyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BusinessProductListRelyVO businessProductListGetRely = businessProductService.businessProductListGetRely(businessProductListRelyDTO);
                response = new XbbResponse<>(businessProductListGetRely);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    /**
     * 选择产品批次
     *
     * @param br BindingResult
     * @return String
     */
    @RequestMapping(value = "/batchList/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getProductBatchs(@RequestBody @Valid BusinessProductBatchListDTO businessProductBatchListDTO, BindingResult br) {
        XbbResponse<BusinessProductBatchListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BusinessProductBatchListVO businessProductBatchListVO = businessProductService.getBusinessProductBathList(businessProductBatchListDTO);
                response = new XbbResponse<>(businessProductBatchListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }



    /**
     * 自动匹配产品批次
     *
     * @param br BindingResult
     * @return String
     */
    @RequestMapping(value = "/batchList/autoMatch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getProductAutoMatchBatchs(@RequestBody @Valid BussinessProductBatchAutoMatchDTO bussinessProductBatchAutoMatchDTO, BindingResult br) {
        XbbResponse<BusinessProductBatchListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BusinessProductBatchListVO businessProductBatchListVO = businessProductService.getProductAutoMatchBatch(bussinessProductBatchAutoMatchDTO);
                response = new XbbResponse<>(businessProductBatchListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * 之前是获取产品金额，获取退货退款的退货金额（特殊处理）
     *
     * @param businessProductListDTO
     * @author youli.chen
     * @Date 19:29 2019/3/7
     * @since v1.0
     * @return java.lang.String
     **/
    @RequestMapping(value = "/refund/amount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getRefundAmount(@RequestBody @Valid BusinessProductListDTO businessProductListDTO, BindingResult br) {
        XbbResponse<RefundAmountVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                boolean isDetail = false;
                businessProductListDTO.setDetailGet(isDetail);
                RefundAmountVO refundAmountVO = refundService.getRefundAmount(businessProductListDTO);
                response = new XbbResponse<>(refundAmountVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
